package by.demchenko.sample.webapp.page.home.teachers;

import by.demchenko.sample.datamodel.Person;
import by.demchenko.sample.datamodel.Schedule;
import by.demchenko.sample.datamodel.Schedule_;
import by.demchenko.sample.services.ScheduleService;
import by.demchenko.sample.webapp.app.BasicAuthenticationSession;
import by.demchenko.sample.webapp.page.home.BaseLayout;
import org.apache.wicket.AttributeModifier;
import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.extensions.markup.html.repeater.data.sort.SortOrder;
import org.apache.wicket.extensions.markup.html.repeater.util.SortableDataProvider;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.repeater.Item;
import org.apache.wicket.markup.repeater.data.DataView;
import org.apache.wicket.model.CompoundPropertyModel;
import org.apache.wicket.model.IModel;
import org.apache.wicket.model.Model;

import javax.inject.Inject;
import javax.persistence.metamodel.SingularAttribute;
import java.util.Iterator;
import java.util.List;

public class StatusPage extends BaseLayout {

    @Inject
    ScheduleService scheduleService;

    private Person teacher;

    public StatusPage() {
        teacher = BasicAuthenticationSession.get().getUser();

        ScheduleDataProvider scheduleDataProvider = new ScheduleDataProvider();

        DataView<Schedule> dataView = new DataView<Schedule>("studentsList", scheduleDataProvider) {
            @Override
            protected void populateItem(Item<Schedule> item) {
                final Schedule schedule = item.getModelObject();
                item.add(new Label("course", schedule.getCourse().getName()));
                item.add(new Label("startDate", schedule.getStartDate()));
                item.add(new Label("status", getString("Status." + schedule.getStatus())));

                Label registeredNumber = new Label("registeredNumber", schedule.getRegistered());

                Model<String> stringModel = Model.of("empty");
                Label registered = new Label("registered", stringModel);
                registered.add(new AttributeModifier("class", new Model<>("logininvisible")));
                registered.setOutputMarkupId(true);

                AjaxLink<Void> link = new AjaxLink<Void>("registeredLink") {
                    @Override
                    public void onClick(AjaxRequestTarget target) {
                        registered.add(new AttributeModifier("class", new Model<>("loginvisible")));

                        List<Person> students = scheduleService.getRegisteredStudents(schedule);

                        StringBuilder sb = new StringBuilder();
                        for (Person s : students) {
                            sb.append(s.getLastName() + " " + s.getFirstName());
                            sb.append(", ");
                        }
                        String s = sb.toString();
                        s = s.substring(0, s.length() - 2);

                        stringModel.setObject(s);
                        target.add(registered);
                        this.setEnabled(false);
                        target.add(this);
                    }
                };

                item.add(link);
                link.add(registeredNumber);
                item.add(registered);
                if (schedule.getRegistered() == 0) {
                    link.setEnabled(false);
                }

                item.add(new Label("total", schedule.getTotal()));
            }
        };

        add(dataView);
    }

    private class ScheduleDataProvider extends SortableDataProvider<Schedule, SingularAttribute<Schedule, ?>> {

        public ScheduleDataProvider() {
            super();
            setSort(Schedule_.startDate, SortOrder.ASCENDING);
        }

        @Override
        public Iterator<? extends Schedule> iterator(long l, long l1) {
            SingularAttribute<Schedule, ?> sortParam = getSort().getProperty();
            SortOrder propertySortOrder = getSortState().getPropertySortOrder(sortParam);
            //boolean ascending = SortOrder.ASCENDING.equals(propertySortOrder);
            return scheduleService.getByTeacher(BasicAuthenticationSession.get().getUser()).iterator();
        }

        @Override
        public long size() {
            return scheduleService.getCountByTeacher(teacher);
        }

        @Override
        public IModel<Schedule> model(Schedule student) {
            return new CompoundPropertyModel<>(student);
        }
    }
}
